<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        /* 全局执行上下文 */
        console.log(a1, window.a1); //undefined undefined
        a2(); //a2
        console.log(this); //window
        var a1 = 3;
        function a2() {
            console.log('a2');
        }
        console.log(a1); //3

        /* 函数执行上下文 */
        function fn(a1) {
            console.log(a1); //2
            console.log(a2); //undefined
            a3(); //a3
            console.log(this); //window
            console.log(arguments); //2 3

            var a2 = 3;
            function a3() {
                console.log('a3');
            }
        }
        fn(2, 3, 4, 'hey');

        /* 执行上下文栈 */
        (function () {
            var a = 10;
            var bar = function (x) {
                var b = 5;
                fun(x + b);
            };
            var fun = function (y) {
                var c = 5;
                console.log(a + c + y);
            };
            bar(10); //30
        })();

        /* 测试题 */
        //1
        console.log('global begin:' + i);
        var i = 1;
        foo(1);
        function foo(i) {
            if (i == 4) {
                return;
            }
            console.log('foo begin:' + i);
            foo(i + 1);
            console.log('foo end:' + i);
        }
        console.log('global end:' + i);
        // undefined 1 2 3 3 2 1 1
        // 执行上下文 = 5 (函数4 + 全局1)

        /*         function foo(1) {
                    console.log('foo begin:' + 1);
                    foo(2){
                        console.log('foo begin:' + 2);
                        foo(3){
                            console.log('foo begin:' + 3);
                            foo(4){
                                if (i == 4) {
                                    return;
                                }
                            }
                            console.log('foo end:' + 3);
                        }
                        console.log('foo end:' + 2);
                    }
                    console.log('foo end:' + 1);
                } */

        //2
        function a(){
            console.log('1');
        }
        function a(){
            console.log('2');
        }
        var a;
        console.log(typeof a); //function
        a(); //2

        //3
        if (!(b in window)){
            var b = 1;
        }
        console.log(b); //undefined

        //4
        var c = 1;
        function c(c){
            console.log(c);
        }
        // console.log(c); //1
        // c(2); //is not a function


    </script>
</body>

</html>